<?php
session_start();
$regionID = $_POST['region_id'];
$countryID = $_POST['country_id'];
$userID = $_SESSION['auth'];

include "class/Region.class.php";
include "inc_/sup3rs3cr3tt3xt4m3t0kn0vv.php";

$mysqli = new mysqli($host, $user, $password, $database);
if (mysqli_connect_errno()) {
	printf("Connect failed: %s\n", mysqli_connect_error());
	exit();
}

$sql = "SELECT region_name, region_lat, region_long FROM regions WHERE country_id = '$countryID' AND region_id = '$regionID'";
if($result = $mysqli->query($sql)) {
	if($result->num_rows != 1) {
		echo "Error";
	} else {
		while($row = $result->fetch_array(MYSQLI_ASSOC)) {
			extract($row);
			$regionName = $region_name;
			$regionLat = $region_lat;
			$regionLong = $region_long;
		}
	}
	$result->close();	
}

$sql = "SELECT country_name FROM countries WHERE country_id = '$countryID'";
if($result = $mysqli->query($sql)) {
	if($result->num_rows != 1) {
		echo "Error";
	} else {
		while($row = $result->fetch_array(MYSQLI_ASSOC)) {
			extract($row);
			$countryName = $country_name;
		}
	}
}
$result->close();

$region = new region();
$result = $region->add_region_as_visited($regionID, $userID);

$markerInfo = array(
	"country" => $countryName,
	"region" => $regionName,
	"regionID" => $regionID,
	"latitude" => $regionLat,
	"longitude" => $regionLong,
);
if($result == 2) {
	echo $result;
} else {
	echo json_encode($markerInfo);
}
?>